package com.colorlight.cloud.complete.entity.event;

import com.colorlight.cloud.complete.api.CompleteEntityEvent;
import com.colorlight.cloud.complete.entity.po.SenderInfo;
import com.colorlight.iot.proto.complete.event.CompleteEvent;
import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 定时上报 发送卡监控数据上报
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
@Slf4j
public class SenderMonitorReport implements CompleteEntityEvent {

    /**
     * 发送卡详情
     */
    private List<SenderInfo> senderInfo;

    @Override
    public SenderMonitorReport byteConvertObject(ByteString bytes) {
        CompleteEvent.SenderMonitorReport senderMonitorReport = null;
        try {
            senderMonitorReport = CompleteEvent.SenderMonitorReport.parseFrom(bytes);
        } catch (InvalidProtocolBufferException e) {
            log.error("", e);
        }
        Objects.requireNonNull(senderMonitorReport);
        List<SenderInfo> collect = senderMonitorReport.getSenderInfoList().stream()
                .map(e -> new SenderInfo().formProto(e)).collect(Collectors.toList());
        this.setSenderInfo(collect);
        return this;
    }
}

